package com.team.six.bluecatshop.controller;

import com.team.six.bluecatshop.service.ProductService;
import com.team.six.bluecatshop.service.ProductTypeService;
import com.team.six.bluecatshop.service.SkuService;
import com.team.six.bluecatshop.utils.RedisUtil;
import com.team.six.bluecatshop.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;

@Controller
@RequestMapping("/test")
public class TestController {
    @Autowired
    private ProductService productService;
    @Autowired
    SkuService skuService;
    @Autowired
    ProductTypeService productTypeService;

    @Resource
    private RedisUtil redisUtil;

    @GetMapping("/indentDetails")
    public String indentDetails(){
        return "indentDetails";
    }

    @RequestMapping("/list")
    public String index(){
        return "administration/Product_list";
    }

    @RequestMapping("/test")
    public String test(Model model){
        return "administration/indent_list";
    }

    @RequestMapping("/testpage")
    public String cart(){
        return "account-information";
    }

    @RequestMapping("/product")
    public String product(){
        return "account-information";
    }

    /**
     * 文件上传下载测试--------------------------
     */
    @RequestMapping("/test1")
    public String test1(Model model) {
        return "test";
    }


    private String upload;
    /**
     * 初始化文件上传路径
     * */
    @PostConstruct
    public void init() {
        try {
            // WEB-INF/classes
            String path = this.getClass().getResource("/").toURI().getPath();

            File dir = new File(path, "../../src/main/resources/upload").getCanonicalFile();
            if (!dir.exists() || !dir.isDirectory()) {
                dir.mkdir();
            }
            upload = dir.getCanonicalPath();
            //System.out.println(upload);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件上传
     * */
    @RequestMapping("file_upload")
    public String upload( @RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) { //选择了文件
            long size = file.getSize();
            String fname = file.getOriginalFilename(); //文件名称
            System.out.println(fname + ", size=" + size);

            // 将文件写入服务器
            File dstFile = new File(upload, fname);
            try {
                file.transferTo(dstFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        return "hello";
    }

    /**
     * 文件下载
     * */
    @RequestMapping("/download")
    public void download(String fname, HttpServletResponse resp) throws Exception {
        fname = URLDecoder.decode(fname, "UTF-8");
        // 设置文件下载头信息
        ServletUtils.setFileDownloadHeader(resp, fname);

        try {
            InputStream is = new FileInputStream(new File(upload, fname));
            OutputStream out = resp.getOutputStream();
            byte[] b = new byte[1024];
            int len;
            while((len = is.read(b)) > 0) {
                out.write(b, 0, len);
            }
            out.flush();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //--------------------------------------------
    /**
     * 测试Redis
     * */
    @RequestMapping("set")
    @ResponseBody
    public boolean redisSet() {
        String key = "test";
        String value = "hello";
        return redisUtil.set(key,value);
    }
    @RequestMapping("get")
    @ResponseBody
    public String redisGet() {
        String key = "test";
        return (String) redisUtil.get(key);
    }
}
